package com.dragon.client.controller.supplies;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.supplies.IProductService;
import com.dragon.repository.domain.dto.ProductDTO;
import com.dragon.repository.domain.vo.ProductVO;
import com.dragon.repository.entity.Product;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品表（SPU） 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-09-11
 */
@RestController
@RequestMapping("product")
@Api(tags = "物资SPU")
public class ProductController {

    @Autowired
    IProductService service;

    @GetMapping("list")
    @ApiOperation(value = "物资列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type = "int"),
            @ApiImplicitParam(name = "categoryId", value = "类别id", required = false, type = "int"),
            @ApiImplicitParam(name = "search", value = "搜索参数(名称)", required = false, type = "string")
    })
    public ResponseData<List<ProductDTO>> listProduct(@RequestParam Integer page,
                                                      @RequestParam Integer limit,
                                                      @RequestParam(required = false) Integer categoryId,
                                                      @RequestParam(required = false) String search){
        Page<Product> productPage = service.listProduct(page,limit,categoryId,search);
        return ResponseData.success(ProductDTO.convertList(productPage.getRecords()), productPage.getTotal());
    }

    @PostMapping("save")
    @ApiOperation(value = "添加物资")
    public ResponseData saveProduct(@RequestBody @Validated(Insert.class) ProductVO vo){
        service.saveProduct(ProductVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "修改")
    public ResponseData updateProduct(@RequestBody @Validated(Update.class) ProductVO vo){
        service.updateProduct(ProductVO.convert(vo));
        return ResponseData.success();
    }
    @PostMapping("delete")
    @ApiOperation(value = "删除")
    public ResponseData deleteProduct(Integer id){
        service.deleteProduct(id);
        return ResponseData.success();
    }

    @PostMapping("detailById")
    @ApiOperation(value = "根据id查看详情")
    public ResponseData<ProductDTO> detailById(@ApiParam(name = "id", value = "id", required = true, type = "int")
                                               @RequestParam Integer id){
        Product product = service.detailById(id);
        return ResponseData.success(ProductDTO.convert(product));
    }
}
